var yz = "http://8.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=40&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=i:1.000001,i:0.399001,i:0.399005,i:0.399006,i:1.000300,i:100.HSI,i:100.HSCEI,i:124.HSCCI,i:100.TWII,i:100.N225,i:100.KOSPI200,i:100.KS11,i:100.STI,i:100.SENSEX,i:100.KLSE,i:100.SET,i:100.PSI,i:100.KSE100,i:100.VNINDEX,i:100.JKSE,i:100.CSEALL&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152,f124,f107&_=";
var mz = "http://8.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=30&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=i:100.DJIA,i:100.SPX,i:100.NDX,i:100.TSX,i:100.BVSP,i:100.MXX&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152,f124,f107&_="
var oz = "http://8.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=30&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=i:100.SX5E,i:100.FTSE,i:100.MCX,i:100.AXX,i:100.FCHI,i:100.GDAXI,i:100.RTS,i:100.IBEX,i:100.PSI20,i:100.OMXC20,i:100.BFX,i:100.AEX,i:100.WIG,i:100.OMXSPI,i:100.SSMI,i:100.HEX,i:100.OSEBX,i:100.ATX,i:100.MIB,i:100.ASE,i:100.ICEXI,i:100.PX,i:100.ISEQ,i:100.AS51,i:100.AORD,i:100.NZ50&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152,f124,f107&_="
var az = "http://90.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=30&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=i:100.AS51,i:100.AORD,i:100.NZ50&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152,f124,f107&_=";
var globalRes = {}
var stockLink = {
    "Sri Lanka": ["CSEALL"],
    "China": ["000001", "399001", "399006", "399005", "HSCCI", "000300", "TWII", "HSCEI", "HSI"],
    "Indonesia": ["JKSE"],
    "Thailand": ["SET"],
    "India": ["SENSEX"],
    "Philippines": ["SENSEX"],
    "Singapore": ["STI"],
    "Pakistan": ["KSE100"],
    "Vietnam": ["VNINDEX"],
    "Korea": ["KS11", "KOSPI200"],
    "Malaysia": ["KLSE"],
    "Japan": ["N225"],
    "United States": ["DJIA", "NDX", "SPX"],
    "Brazil": ["BVSP"],
    "Canada": ["TSX"],
    "Mexico": ["MXX"],
    "Spain": ["IBEX"],
    "Ireland": ["ISEQ"],
    "Russia": ["RTS"],
    "Italy": ["MIB"],
    "Czech Rep.": ["PX"],
    "United Kingdom": ["FTSE", "MCX"],
    "Austria": ["ATX"],
    "Greece": ["ASE"],
    "Poland": ["WIG"],
    "Belgium": ["BFX"],
    "Iceland": ["ICEXI"],
    "France": ["FCHI"],
    "Portugal": ["PSI20"],
    "Finland": ["HEX"],
    "Netherlands": ["AEX"],
    "Sweden": ["OMXSPI"],
    "Germany": ["GDAXI"],
    "Australia": ["AS51", "AORD"],
    "New Zealand": ["NZ50"]
}
var stockSync = {
    canNext: true,
    get: () => {
        return globalRes
    },
    initData: () => {
        stockSync.getData();
        setInterval(() => {
            stockSync.setTime();
            stockSync.getData();
        }, 1000)
    },
    initTime: () => {
        stockSync.setTime();
        setInterval(() => {
        }, 1000)
    },
    setTime: () => {
        let date = new Date();
        let hsdDate = new Date(date.setHours(date.getHours() - 12))
        date = new Date();
        let ldDate = new Date(date.setHours(date.getHours() - 8))
        date = new Date();
        let time = document.getElementById("time")
        let hsd_time = document.getElementById("hsd_time")
        let ld_time = document.getElementById("ld_time")

        let hou = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
        let min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        let sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        time.innerHTML = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${hou}:${min}:${sec}`;

        hou = hsdDate.getHours() < 10 ? '0' + hsdDate.getHours() : hsdDate.getHours();
        min = hsdDate.getMinutes() < 10 ? '0' + hsdDate.getMinutes() : hsdDate.getMinutes();
        sec = hsdDate.getSeconds() < 10 ? '0' + hsdDate.getSeconds() : hsdDate.getSeconds();
        hsd_time.innerHTML = `${hsdDate.getFullYear()}-${hsdDate.getMonth() + 1}-${hsdDate.getDate()} ${hou}:${min}:${sec}`;

        hou = ldDate.getHours() < 10 ? '0' + ldDate.getHours() : ldDate.getHours();
        min = ldDate.getMinutes() < 10 ? '0' + ldDate.getMinutes() : ldDate.getMinutes();
        sec = ldDate.getSeconds() < 10 ? '0' + ldDate.getSeconds() : ldDate.getSeconds();
        ld_time.innerHTML = `${ldDate.getFullYear()}-${ldDate.getMonth() + 1}-${ldDate.getDate()} ${hou}:${min}:${sec}`;
    },
    getData: () => {
        //console.log("sync stock data")
        if (!stockSync.canNext) {
            return;
        }
        stockSync.canNext = false;
        let allData = [];
        Ajax.get(yz + (new Date().getTime()), (result) => {
            result = JSON.parse(result)
            if (result && result.data && result.data.diff) {
                allData = allData.concat(result.data.diff);
            }
        })
        Ajax.get(mz + (new Date().getTime()), (result) => {
            result = JSON.parse(result)
            if (result && result.data && result.data.diff) {
                allData = allData.concat(result.data.diff);
            }
        })
        Ajax.get(oz + (new Date().getTime()), (result) => {
            result = JSON.parse(result)
            if (result && result.data && result.data.diff) {
                allData = allData.concat(result.data.diff);
            }
        })
        /*Ajax.get(az + (new Date().getTime()), (result) => {
            result = JSON.parse(result)
            if (result && result.data && result.data.diff) {
                allData = allData.concat(result.data.diff);
            }
        })*/
        //
        let stopData = {};
        allData.forEach((item, index) => {
            //console.log(index, JSON.stringify(item))
            stopData[item['f12']] = item;
        })
        var res = {}
        for (let stockLinkKey in stockLink) {
            res[stockLinkKey] = []
            stockLink[stockLinkKey].forEach((item, index) => {
                if (stopData[item]) {
                    res[stockLinkKey].push(stopData[item])
                }
            })
        }
        //加载数据 回显数据
        for (let ck in res) {
            globalRes[ck] = res[ck];
        }
        stockData = globalRes;
        for (let ck in stockData) {
            stockData[ck].forEach((item, index) => {
                stockDataMap[item["f12"]] = item;
            })
        }
        //初始化
        var tempData = [];
        data_tmp.forEach((item, index) => {
            if (stockLinkParent[item.name]) {
                let value = 0;
                let sd = stockData[item.name] || [];
                stockLinkParent[item.name].forEach((item1, index1) => {
                    if (stockDataMap[item1]) {
                        value += stockDataMap[item1]["f3"];
                    }
                })
                item.value = value;
                item.sd = sd;
            }
            tempData.push(item);
        })
        data_tmp = tempData;
        var tempOption = myChart.getOption();
        if (tempOption.series.length == 1) {
            tempOption.series[0].data = data_tmp
        } else if (tempOption.series.length == 2) {
            tempOption.series[0].data = convertData(data_tmp)
            tempOption.series[1].data = data_tmp
        }
        myChart.setOption(tempOption);
        stockSync.canNext = true;
    }
}
stockSync.initData();
stockSync.initTime();

